package stdio.costa.moca.rest;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import stdio.costa.moca.model.Img;
import stdio.costa.utils.ImageUtils;

@Path("/img")
public class ImageResource extends RestfulOperations {
	
	@Path("/{uuid}")
	@GET
	@Produces({ "image/jpeg", "image/png" })
	public Response getImg(@PathParam("uuid") String uuid, @QueryParam("width") Integer width, @QueryParam("height") Integer height)
			throws IOException {
		logger.info("Getting image...");
		Img image = getEntityManager().find(Img.class, uuid);
		logger.info("Image loaded, size {}", image.getData().length);
		byte[] bytes = image.getData();
		if (width != null && height != null) {
			bytes = ImageUtils.zoom(image.getData(), width, height);
		}
		return Response.ok(new ByteArrayInputStream(bytes)).build();
	}
}
